﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Domain;
using Cms.Web.Framework.Controls;
using iLib.Web.UI;

namespace Cms.Web.UI.Admin
{
    public partial class Users : AdminPage, IUsersView
    {
        private UsersPresenter _presenter;

        #region IUsersView Members

        public void SetUserGroups(IList<UserGroup> groups)
        {
            ddlUserGroups.Items.Clear();
            ddlUserGroups.Items.Add(new ListItem(GetText(this, "AllUserGroups"), "-1"));
            foreach (UserGroup group in groups)
            {
                ddlUserGroups.Items.Add(new ListItem(group.Name, group.ID.ToString()));
            }
        } 

        public int SelectedUserGroupId
        {
            get { return int.Parse(ddlUserGroups.SelectedValue); }
            set { ddlUserGroups.SelectedValue = value.ToString(); }
        }

        public void SetUsers(IList<User> users)
        {
            rpUsers.DataSource = users;
            rpUsers.DataBind();
            trNoHits.Visible = users.Count == 0;
        }

        public string ContextTranslationFile
        {
            get { return "Admin/Translations/Users.xml"; }
        }

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _presenter = new UsersPresenter(this);
            HeadingText = GetText(this, "Heading");
            if (!Page.IsPostBack)
            {
                _presenter.Initialize();
                HeadingIcon = litIcon.Text;
            }
            lbDelete.OnClientClick = "return cms.Dialogs.ConfirmLinkButton(this, " +
                                     "function (status) { " +
                                     "if (status == 'ok') " +
                                     "cms.Lists.ShowPaneOverlay('userPaneOverlay');" +
                                     "else " +
                                     "cms.Lists.HidePaneOverlays(); " +
                                     "}, " +
                                     "'" + GetText(this, "DeleteHeader") + "', " +
                                     "'" + GetText(this, "DeleteMessage") + "', " +
                                     "'" + GetText(this, "DeleteOK") + "', " +
                                     "'" + GetText(this, "DeleteCancel") + "', " +
                                     "true, " +
                                     "cms.Lists.CurrentName);";
        }

        protected void rpUsers_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
            {
                return;
            }
            var user = (User)e.Item.DataItem;
            var litId = (Literal)e.Item.FindControl("litId");
            var litUsername = (Literal)e.Item.FindControl("litUsername");
            var litPhonebookName = (Literal)e.Item.FindControl("litPhonebookName");
            var litEmail = (Literal)e.Item.FindControl("litEmail");
            var litSystemId = (Literal)e.Item.FindControl("litSystemId");
            litId.Text = "<span id=\"U" + user.ID + "\">";
            litUsername.Text = user.Username;
            litPhonebookName.Text = user.PhonebookName;
            litEmail.Text = user.Email;
            litSystemId.Text = user.ID.ToString();
        }


        protected void lbDelete_Click(object sender, EventArgs e)
        {
            _presenter.DeleteUser(int.Parse(hidDeleteID.Value.Substring(1)));
            upnlUsers.Update();
            ClientScriptProxy.Current.RegisterStartupScript(pnlActions, GetType(), "initList", "initList();", true);
        }
    }
}